package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 根据id查询套餐
     */
    @Override
    public SetmealVO getById(Long id) {
        //查询套餐基本信息
        SetmealVO setmealVO = setmealMapper.selectById(id);
        //查询套餐和菜品的关联关系
        setmealVO.setSetmealDishes(setmealDishMapper.getBySetmealId(id));
        return setmealVO;
    }

    /**
     * 修改套餐
     */
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        //修改套餐基础信息
        setmealDTO.setCreateTime(LocalDateTime.now());
        setmealDTO.setUpdateTime(LocalDateTime.now());
        setmealMapper.updateSetmeal(setmealDTO);
        //修改套餐和菜品的关联关系
        //先删除所有的关联关系
        setmealDishMapper.deleteById(setmealDTO.getId());
        //创建新的关联关系
        for (SetmealDish sd : setmealDTO.getSetmealDishes()) {
            sd.setId(setmealDTO.getId());
        }

        //添加套餐和菜品的关联关系
        setmealDishMapper.insertSetmealDish(setmealDTO.getSetmealDishes());
    }

    /**
     * 分页条件查询
     */
    @Override
    public PageResult selectPage(SetmealPageQueryDTO query) {
        //开启分页
        PageHelper.startPage(query.getPage(), query.getPageSize());

        List<SetmealVO> list = setmealMapper.selectPage(query);
        Page<SetmealVO> p = (Page<SetmealVO>) list;
        return new PageResult(p.getTotal(), p.getResult());
    }

    /**
     * 新增套餐
     */
    @Transactional
    @Override
    public void insertSetmeal(SetmealDTO setmealDTO) {
        //补全时间
        setmealDTO.setCreateTime(LocalDateTime.now());
        setmealDTO.setUpdateTime(LocalDateTime.now());
        //先添加套餐的基本信息
        setmealMapper.insertSetmeal(setmealDTO);
        for (SetmealDish sd : setmealDTO.getSetmealDishes()) {
            sd.setId(setmealDTO.getId());
        }

        //添加套餐和菜品的关联关系
        setmealDishMapper.insertSetmealDish(setmealDTO.getSetmealDishes());
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void deleteByIds(List<Integer> ids) {
        //先删除套餐的基本信息
        setmealMapper.deleteByIds(ids);

        //删除套餐和菜品的关联关系
        setmealDishMapper.deleteByIds(ids);
    }


    /**
     * 套餐起售、停售
     * 修改套餐状态
     */
    @Override
    public void updateStatusById(Long id) {
        setmealMapper.updateStatusById(id);
    }

}
